<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px; margin:0px; text-align:center}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

$matchId = $_GET["matchId"];
$matchOptaId = $_GET["matchOptaId"];
$matchCategory = $_GET["matchCategory"];
$matchOptaSeason = $_GET["matchOptaSeason"];
$matchOptaRound = $_GET["matchOptaRound"];
@ $date = $_GET["date"];

if ($matchId == "") {
	return;
}
if ($date == "") {
	$date = date("Y-m-d");
}

//先清空当前比赛场次的数据
$sql = "delete from live_member_stats_f where matchId = '" . $matchId . "' or  matchId = '" . $matchOptaId . "';";
$sql .= "INSERT INTO live_member_stats_f (matchId, team, first, id, name, c_q, q_d, t_q, l_j, g_r, f_g, b_f, y_w, z_g, s_m, p_j, h_h_p, j_q_s) VALUES";

if ($matchOptaId != "") {
	$matchId = $matchOptaId;
	$url = "http://goal.sports.163.com/" . $matchOptaRound . "/match/player/" . $matchOptaSeason . "/" . $matchId . ".json?" . rand();
	$memberStats = file_get_contents($url);
	$memberStats = json_decode($memberStats);
	$memberStats = $memberStats[0];
	$homePlayer = $memberStats->homePlayer;
	$awayPlayer = $memberStats->awayPlayer;

	foreach ($homePlayer as $player) {
		if ($player->gameStarted == "1") {
			$player->gameStarted = 'Y';
		} else {
			$player->gameStarted = 'N';
		}

		if (!isset ($player->accuratePass)) {
			$player->accuratePass = "-";
		}
		if (!isset ($player->totalPass)) {
			$player->totalPass = "-";
		}

		if (!isset ($player->wonTackle)) {
			$player->wonTackle = "-";
		}
		if (!isset ($player->totalTackle)) {
			$player->totalTackle = "-";
		}

		if (!isset ($player->aerialWon)) {
			$player->aerialWon = 0;
		}
		if (!isset ($player->aerialLost)) {
			$player->aerialLost = 0;
		}

		if (!isset ($player->interception)) {
			$player->interception = "-";
		}

		if (!isset ($player->wonContest)) {
			$player->wonContest = "-";
		}
		if (!isset ($player->totalContest)) {
			$player->totalContest = "-";
		}

		if (!isset ($player->fouls)) {
			$player->fouls = "-";
		}

		if (!isset ($player->wasFouled)) {
			$player->wasFouled = "-";
		}

		if (!isset ($player->totalOffside)) {
			$player->totalOffside = "-";
		}

		if (!isset ($player->goalAssist)) {
			$player->goalAssist = "-";
		}

		if (!isset ($player->totalScoringAtt)) {
			$player->totalScoringAtt = "-";
		}
		if (!isset ($player->ontargetScoringAtt)) {
			$player->ontargetScoringAtt = "-";
		}

		if (!isset ($player->saves)) {
			$player->saves = "-";
		}

		if (!isset ($player->redCard)) {
			$player->redCard = "-";
		}
		if (!isset ($player->yellowCard)) {
			$player->yellowCard = "-";
		}

		if (!isset ($player->goals)) {
			$player->goals = "-";
		}

		$sql .= "('" . $matchId . "', '1', " .
		"'" . $player->gameStarted . "',  " .
		"'" . $player->jerseyNum . "', " .
		"'" . $player->player . "', " .
		"'" . $player->accuratePass . "/" . $player->totalPass . "', " .
		"'" . $player->wonTackle . "/" . $player->totalTackle . "', " .
		"'" . $player->aerialWon . "/" . ($player->aerialWon + $player->aerialLost) . "', " .
		"'" . $player->interception . "', " .
		"'" . $player->wonContest . "/" . $player->totalContest . "', " .
		"'" . $player->fouls . "', " .
		"'" . $player->wasFouled . "', " .
		"'" . $player->totalOffside . "', " .
		"'" . $player->goalAssist . "', " .
		"'" . $player->ontargetScoringAtt . "/" . $player->totalScoringAtt . "', " .
		"'" . $player->saves . "', " .
		"'" . $player->redCard . "/" . $player->yellowCard . "', " .
		"'" . $player->goals . "'),";
	}
	foreach ($awayPlayer as $player) {
		if ($player->gameStarted == "1") {
			$player->gameStarted = 'Y';
		} else {
			$player->gameStarted = 'N';
		}

		if (!isset ($player->accuratePass)) {
			$player->accuratePass = "-";
		}
		if (!isset ($player->totalPass)) {
			$player->totalPass = "-";
		}

		if (!isset ($player->wonTackle)) {
			$player->wonTackle = "-";
		}
		if (!isset ($player->totalTackle)) {
			$player->totalTackle = "-";
		}

		if (!isset ($player->aerialWon)) {
			$player->aerialWon = 0;
		}
		if (!isset ($player->aerialLost)) {
			$player->aerialLost = 0;
		}

		if (!isset ($player->interception)) {
			$player->interception = "-";
		}

		if (!isset ($player->wonContest)) {
			$player->wonContest = "-";
		}
		if (!isset ($player->totalContest)) {
			$player->totalContest = "-";
		}

		if (!isset ($player->fouls)) {
			$player->fouls = "-";
		}

		if (!isset ($player->wasFouled)) {
			$player->wasFouled = "-";
		}

		if (!isset ($player->totalOffside)) {
			$player->totalOffside = "-";
		}

		if (!isset ($player->goalAssist)) {
			$player->goalAssist = "-";
		}

		if (!isset ($player->totalScoringAtt)) {
			$player->totalScoringAtt = "-";
		}
		if (!isset ($player->ontargetScoringAtt)) {
			$player->ontargetScoringAtt = "-";
		}

		if (!isset ($player->saves)) {
			$player->saves = "-";
		}

		if (!isset ($player->redCard)) {
			$player->redCard = "-";
		}
		if (!isset ($player->yellowCard)) {
			$player->yellowCard = "-";
		}

		if (!isset ($player->goals)) {
			$player->goals = "-";
		}

		$sql .= "('" . $matchId . "', '2', " .
		"'" . $player->gameStarted . "',  " .
		"'" . $player->jerseyNum . "', " .
		"'" . $player->player . "', " .
		"'" . $player->accuratePass . "/" . $player->totalPass . "', " .
		"'" . $player->wonTackle . "/" . $player->totalTackle . "', " .
		"'" . $player->aerialWon . "/" . ($player->aerialWon + $player->aerialLost) . "', " .
		"'" . $player->interception . "', " .
		"'" . $player->wonContest . "/" . $player->totalContest . "', " .
		"'" . $player->fouls . "', " .
		"'" . $player->wasFouled . "', " .
		"'" . $player->totalOffside . "', " .
		"'" . $player->goalAssist . "', " .
		"'" . $player->ontargetScoringAtt . "/" . $player->totalScoringAtt . "', " .
		"'" . $player->saves . "', " .
		"'" . $player->redCard . "/" . $player->yellowCard . "', " .
		"'" . $player->goals . "'),";
	}
} else {
	//国际足球
	if ($matchCategory == "2") {
		$url = "http://data.sports.sina.com.cn/livecast/js/lineup_" . $matchId . ".js?" . rand();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		$memberStats = curl_exec($ch);

		curl_close($ch);
		$memberStats = iconv("GBK", "UTF-8", $memberStats);
		$memberStats = str_replace("var l1 = [", "", $memberStats);
		$memberStats = str_replace("var l2 = [", "", $memberStats);
		$memberStats = str_replace("'", "", $memberStats);
		$memberStats = str_replace("]", "", $memberStats);
		$memberStats = str_replace("show_lineup();", "", $memberStats);

		$memberStats = explode(";", $memberStats);
		$homePlayer = $memberStats[0];
		$homePlayer = explode(",", $homePlayer);
		$awayPlayer = $memberStats[1];
		$awayPlayer = explode(",", $awayPlayer);

		foreach ($homePlayer as $player) {
			if ($player == "") {
				continue;
			}
			$info = explode("*", $player);
			$gameStarted = "N";
			if ($info[0] == "Z") {
				$gameStarted = "Y";
			}
			if ($info[3] == "蒂亚戈") {
				$info[2] = "11";
			}
			if ($info[3] == "马蒂厄") {
				$info[2] = "22";
			}
			$sql .= "('" . $matchId . "', '1', " .
			"'" . $gameStarted . "',  " .
			"'" . $info[2] . "', " .
			"'" . $info[3] . "', " .
			"NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),";
		}
		foreach ($awayPlayer as $player) {
			if ($player == "") {
				continue;
			}
			$info = explode("*", $player);
			$gameStarted = "N";
			if ($info[0] == "Z") {
				$gameStarted = "Y";
			}
			if ($info[3] == "马蒂厄") {
				$info[2] = "22";
			}
			if ($info[3] == "蒂亚戈") {
				$info[2] = "11";
			}
			$sql .= "('" . $matchId . "', '2', " .
			"'" . $gameStarted . "',  " .
			"'" . $info[2] . "', " .
			"'" . $info[3] . "', " .
			"NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),";
		}
	} else {
		//国内足球
		$url = "http://sports.sinajs.cn/livecast/js/lineup_" . $matchId . ".js?" . rand();

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		$memberStats = curl_exec($ch);

		curl_close($ch);
		$memberStats = iconv("GBK", "UTF-8", $memberStats);
		$memberStats = str_replace("var l1 = [", "", $memberStats);
		$memberStats = str_replace("var l2 = [", "", $memberStats);
		$memberStats = str_replace("'", "", $memberStats);
		$memberStats = str_replace("]", "", $memberStats);
		$memberStats = str_replace("show_lineup();", "", $memberStats);

		$memberStats = explode(";", $memberStats);

		$homePlayer = $memberStats[0];
		$homePlayer = explode(",", $homePlayer);

		$awayPlayer = $memberStats[1];
		$awayPlayer = explode(",", $awayPlayer);

		foreach ($homePlayer as $player) {
			if ($player == "") {
				continue;
			}
			$info = explode("*", $player);
			$gameStarted = "N";
			if ($info[0] == "Z") {
				$gameStarted = "Y";
			}
			$sql .= "('" . $matchId . "', '1', " .
			"'" . $gameStarted . "',  " .
			"'" . $info[2] . "', " .
			"'" . $info[3] . "', " .
			"NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),";
		}
		foreach ($awayPlayer as $player) {
			if ($player == "") {
				continue;
			}
			$info = explode("*", $player);
			$gameStarted = "N";
			if ($info[0] == "Z") {
				$gameStarted = "Y";
			}
			$sql .= "('" . $matchId . "', '2', " .
			"'" . $gameStarted . "',  " .
			"'" . $info[2] . "', " .
			"'" . $info[3] . "', " .
			"NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),";
		}
	}

}

$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

//插入比赛比分
mysqli_multi_query($conn, $sql);

echo "球员技术统计数据采集";
?>